'use strict';

const Controller = require('egg').Controller;

class ConsultationController extends Controller {
    async deleteCourses() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '咨询ids'
            }
        });
        let { ids } = ctx.request.body;
        console.log(ids, 123456);
        ids = JSON.parse(ids);
        await app.model.Consultation.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async updateCourses() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                required: true,
                desc: '咨询id',
            },
            courseimage: {
                type: 'string',
                desc: '咨询背景图',
                default: ''
            },
            coursename: {
                type: 'string',
                desc: '咨询主题',
                default: ''
            },
            coursecopywriting: {
                type: 'string',
                required: true,
                desc: '咨询文案',
                default: ''
            },
            courseharvest: {
                type: 'string',
                allowNull: false,
                defaultValue: '',
                comment: '课程收获',
            },
            courseinformation: {
                type: 'string',
                required: true,
                desc: '报名信息',
            },
            courseprice: {
                type: 'string',
                required: true,
                desc: '课程价格',
            },
            attentionevent: {
                type: 'string',
                required: true,
                desc: '注意事件',
            },
            coursetype: {
                type: 'string',
                required: true,
                desc: '分类',
            },
            teacherarea: {
                type: 'string',
                required: true,
                desc: '地区',
            },
        });
        const { courseimage, coursename, coursecopywriting, courseharvest, courseinformation, courseprice, attentionevent, coursetype, teacherarea, id } = ctx.request.body;

        let courses = await app.model.Consultation.findOne({ id });
        if (!courses) {
            ctx.throw('更新失败,不存在此课程')
        };
        // swiper.image = image;
        // swiper.link = link;
        // swiper.order = order;
        // await swiper.save();
        await app.model.Consultation.update({ courseimage, coursename, coursecopywriting, courseharvest, courseinformation, courseprice, attentionevent, coursetype, teacherarea, id }, {
            where: {
                id
            }
        })
        ctx.apiSuccess('更新成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        let courses = await app.model.Consultation.findOne({
            where: {
                id: id
            }
        });
        ctx.apiSuccess(courses);
    }

    async consultationList() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;

        let course = await app.model.Consultation.findAll();
        // console.log(course);
        let list = course.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);

        ctx.apiSuccess({
            totalCount: course.length,
            currPage: pageNumber,
            list: JSON.parse(JSON.stringify(list))
        });

    }

    async addconsultation() {
        const { ctx, app } = this;
        ctx.validate({
            courseimage: {
                type: 'string',
                desc: '咨询背景图',
                default: ''
            },
            coursename: {
                type: 'string',
                desc: '咨询主题',
                default: ''
            },
            coursecopywriting: {
                type: 'string',
                required: true,
                desc: '咨询文案',
                default: ''
            },
            courseharvest: {
                type: 'string',
                allowNull: false,
                defaultValue: '',
                comment: '课程收获',
            },
            courseinformation: {
                type: 'string',
                required: true,
                desc: '报名信息',
            },
            courseprice: {
                type: 'string',
                required: true,
                desc: '课程价格',
            },
            attentionevent: {
                type: 'string',
                required: true,
                desc: '注意事件',
            },
            coursetype: {
                type: 'string',
                required: true,
                desc: '分类',
            },
            teacherarea: {
                type: 'string',
                required: true,
                desc: '地区',
            },
        });
        const { courseimage, coursename, coursecopywriting, courseharvest, courseinformation, courseprice, attentionevent, coursetype, teacherarea } = ctx.request.body;

        const data = await app.model.Consultation.create({
            courseimage,
            coursename,
            coursecopywriting,
            courseharvest,
            courseinformation,
            courseprice,
            attentionevent,
            coursetype,
            teacherarea
        });
        // console.log(JSON.parse(JSON.stringify(data)));
        ctx.apiSuccess('添加成功')
    };
}

module.exports = ConsultationController;